﻿scene.controls = (function ($, undefined)
{
    var _container = null;
    var _controls = null;
    var _camera = null;
    var _renderFunction = null;
    
    var init = function ($container, camera, renderFunction)
    {
        _container = $container;
        _camera = camera;
        _renderFunction = renderFunction;
        _controls = _createTrackballControls();
    };
    
    var destroy = function ()
    {
        _controls = null;
    };

    var updateControls = function ()
    {
        _controls.update();
    };

    var handleResize = function()
    {
        _controls.handleResize();
    };

    var _createTrackballControls = function ()
    {
        var controls = new THREE.TrackballControls(_camera, _container[0]);

        controls.addEventListener('change', _renderFunction);
        
        return controls;
    };

    var _createFlyControls = function()
    {
        var controls = new THREE.FlyControls(_camera, _container[0]);

        controls.movementSpeed = 2500;
        controls.domElement = _container;
        controls.rollSpeed = Math.PI / 6;
        controls.autoForward = false;
        controls.dragToLook = false;

        return controls;
    };

    // public API
    return {
        init: init,
        destroy: destroy,
        updateControls: updateControls,
        handleResize: handleResize
    };
})(jQuery);